package com.xiaokui.nursing.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.xiaokui.common.utils.DateUtils;
import com.xiaokui.nursing.vo.NursingLevelVo;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.web.header.writers.CacheControlHeadersWriter;
import org.springframework.stereotype.Service;
import com.xiaokui.nursing.mapper.NursingLevelMapper;
import com.xiaokui.nursing.domain.NursingLevel;
import com.xiaokui.nursing.service.INursingLevelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 护理等级Service业务层处理
 * 
 * @author alexis
 * @date 2025-06-02
 */
@Service
@AllArgsConstructor
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper, NursingLevel> implements INursingLevelService
{

    private NursingLevelMapper nursingLevelMapper;
    private RedisTemplate redisTemplate;
    private static final String CACHE_KEY_PREFIX="nursing:level";


    /**
     * 查询护理等级
     *
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询护理等级列表
     *
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevel> selectNursingLevelList(NursingLevel nursingLevel)
    {
        return nursingLevelMapper.selectNursingLevelList(nursingLevel);
    }

    /**
     * 新增护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel)
    {
        nursingLevel.setCreateTime(DateUtils.getNowDate());
        int flag=save(nursingLevel) ? 1 : 0;
        deleteCache();
        return flag;
    }

    /**
     * 修改护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel)
    {
        nursingLevel.setUpdateTime(DateUtils.getNowDate());
        int flag=updateById(nursingLevel) ? 1 : 0;
        deleteCache();
        return flag;
    }

    /**
     * 批量删除护理等级
     *
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelByIds(Long[] ids)
    {
        int flag=removeByIds(Arrays.asList(ids)) ? 1 : 0;
        deleteCache();
        return flag;
    }

    /**
     * 删除护理等级信息
     *
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Long id)
    {
        int flag=removeById(id) ? 1 : 0;
        deleteCache();
        return flag;
    }

    /**
     * 查询护理等级Vo列表
     *
     * @param nursingLevel 条件
     * @return 结果
     */
    @Override
    public List<NursingLevelVo> selectNursingLevelVoList(NursingLevel nursingLevel) {
        return nursingLevelMapper.selectNursingLevelVoList(nursingLevel);
    }

    /**
     * 查询所有护理等级
     * @return
     */
    @Override
    public List<NursingLevel> listAll() {
        //从缓存中获取
        List<NursingLevel> list=(List<NursingLevel>)redisTemplate.opsForValue().get(CACHE_KEY_PREFIX);
        if(list!=null&&list.size()>0){
            return list;
        }
        LambdaQueryWrapper<NursingLevel> queryWrapper=new LambdaQueryWrapper<NursingLevel>();
        queryWrapper.eq(NursingLevel::getStatus,1) ;
        list=list(queryWrapper);
        redisTemplate.opsForValue().set(CACHE_KEY_PREFIX,list);
        return list;
    }

    /**
     * 删除护理等级缓存
     */
    private void deleteCache(){
        redisTemplate.delete(CACHE_KEY_PREFIX);
    }
}
